import 'dart:core';
import 'dart:ui';

import 'package:dk_flutter_base/dk_core.dart';
import 'package:flutter/material.dart';

///TabBar + TabBarView
///相当于 Android 原生TabLayout + ViewPage
///有自带 Material Design 动画
///代码实现简单
///支持左右滑动
class DkTabBar extends TabBar {
  final TabBarOption tabBarOption;

  DkTabBar(this.tabBarOption)
      : super(
          key: tabBarOption?.key,
          controller: tabBarOption?.controller,
          indicator: tabBarOption?.indicator,
          indicatorColor: tabBarOption?.indicatorColor,
          //tab标签的下划线颜色
          indicatorWeight: tabBarOption?.indicatorWeight,
          indicatorPadding: tabBarOption?.indicatorPadding,
          indicatorSize: tabBarOption?.indicatorSize,
          labelColor: tabBarOption?.selectedLabelColor,
          unselectedLabelColor: tabBarOption?.unselectedLabelColor,
          tabs: tabBarOption?.tabs,
        );
}

class TabBarOption {
  Key key;

  ///设置了indicator后indicatorWeight、indicatorPadding、indicatorColor无效
  Decoration indicator;

  ///TabBar的api-底部下划线指示器的颜色
  Color indicatorColor = Colors.black;

  ///TabBar的api-底部导航栏指示器的 高度(权重)
  double indicatorWeight = 2.0;

  ///TabBar的api-底部导航栏指示器的padding
  EdgeInsetsGeometry indicatorPadding = EdgeInsets.zero;

  ///指示器的长度模式 铺满 还是 和widget一致
  TabBarIndicatorSize indicatorSize;

  ///TabBar的api-文字和icon选中时的颜色
  Color selectedLabelColor;

  ///TabBar的api-文字和icon未选中时的颜色
  Color unselectedLabelColor;

  ///底部按钮
  List<Tab> tabs;

  ///只有baseHomePage中可以不传
  TabController controller;

  TabBarOption(
    this.key,
    this.indicator,
    this.indicatorColor,
    this.indicatorWeight,
    this.indicatorPadding,
    this.indicatorSize,
    this.selectedLabelColor,
    this.unselectedLabelColor,
    this.tabs,
    this.controller,
  );

  ///隐藏指示器
  TabBarOption.withNoIndicator(
    this.tabs, {
    this.key,
    this.controller,
    this.selectedLabelColor,
    this.unselectedLabelColor,
  }) {
    indicator = UnderlineTabIndicator(borderSide: BorderSide(width: 0));
    if (listEmpty(tabs)) {
      tabs = List()
        ..add(Tab(
          child: LogicPage.emptyPage(),
        ));
    }
  }

  ///隐藏指示器-路由
  TabBarOption.withRouteNoIndicator(
    List<MapEntry<String, Map<String, dynamic>>> tabRoutes, {
    this.key,
    this.controller,
    this.selectedLabelColor,
    this.unselectedLabelColor,
  }) {
    indicator = UnderlineTabIndicator(borderSide: BorderSide(width: 0));
    initTabsByRoute(tabRoutes);
  }

  ///自定义指示器
  TabBarOption.withTabIndicator(
    this.indicator,
    this.tabs, {
    this.key,
    this.controller,
    this.selectedLabelColor,
    this.unselectedLabelColor,
    this.indicatorSize,
  }) {
    if (listEmpty(tabs)) {
      tabs = List()
        ..add(Tab(
          child: LogicPage.emptyPage(),
        ));
    }
    indicator ??= UnderlineTabIndicator(borderSide: BorderSide(width: 0));
  }

  ///自定义指示器-路由
  TabBarOption.withRouteIndicator(
    this.indicator,
    List<MapEntry<String, Map<String, dynamic>>> tabRoutes, {
    this.key,
    this.controller,
    this.indicatorSize,
  }) {
    initTabsByRoute(tabRoutes);
    indicator ??= UnderlineTabIndicator(borderSide: BorderSide(width: 0));
  }

  ///自定义指示器样式
  TabBarOption.withTabIndicatorStyle(
    this.tabs, {
    this.controller,
    this.indicatorWeight = 2.0,
    this.indicatorPadding = EdgeInsets.zero,
    this.indicatorColor = Colors.black,
    this.indicatorSize,
    this.selectedLabelColor,
    this.unselectedLabelColor,
    this.key,
  }) {
    if (listEmpty(tabs)) {
      tabs = List()
        ..add(Tab(
          child: LogicPage.emptyPage(),
        ));
    }
    if (null == indicatorWeight || indicatorWeight <= 0) {
      indicatorWeight = 2.0;
    }
    indicatorPadding ??= EdgeInsets.zero;
  }

  ///自定义指示器样式-路由
  TabBarOption.withRouteIndicatorStyle(
    List<MapEntry<String, Map<String, dynamic>>> tabRoutes, {
    this.controller,
    this.indicatorWeight,
    this.indicatorPadding = EdgeInsets.zero,
    this.indicatorColor = Colors.black,
    this.indicatorSize,
    this.key,
  }) {
    initTabsByRoute(tabRoutes);
    if (null == indicatorWeight || indicatorWeight <= 0) {
      indicatorWeight = 2.0;
    }
    indicatorPadding ??= EdgeInsets.zero;
  }

  void initTabsByRoute(List<MapEntry<String, Map<String, dynamic>>> tabRoutes) {
    tabs = List<Tab>();
    if (listEmpty(tabRoutes)) {
      tabs.add(Tab(
        child: LogicPage.emptyPage(),
      ));
      return;
    }
    tabRoutes.forEach((MapEntry<String, Map<String, dynamic>> mapEntry) {
      if (null != mapEntry) {
        tabs.add(
            RouteUtil.getWidget(mapEntry.key, widgetParams: mapEntry.value));
      }
    });
  }
}
